package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.userms.dto.ManageBatchDeleteImageDTO;
import com.yuanfeng.userms.dto.ManageImageNameUpdateParamDTO;
import com.yuanfeng.userms.dto.ManageImageSpaceQueryDTO;
import com.yuanfeng.userms.dto.ManageImageUploadParamDTO;
import com.yuanfeng.userms.entity.UserUploadBaseEntity;
import com.yuanfeng.userms.mapper.UserUploadBaseMapper;
import com.yuanfeng.userms.service.UserUploadBaseService;
import com.yuanfeng.userms.vo.ManageUploadBaseQueryResultVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserUploadBaseServiceImpl
 * @Description 用户附件表-图片、视频
 * @Author cf
 * @Date 2023/6/29 9:14
 * @Version 1.0
 */
@Service
public class UserUploadBaseServiceImpl extends ServiceImpl<UserUploadBaseMapper, UserUploadBaseEntity> implements UserUploadBaseService {

    @Resource
    private UserUploadBaseMapper userUploadBaseMapper;

    @Override
    public ResponseResult queryImageSpace(ManageImageSpaceQueryDTO param) throws IllegalAccessException {

        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_ALBUM,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            param.setShopId(userPermissionsResult.getShopId());
            //查询上传的附件信息
            Map data= ParseMapUtils.beanToMap(param);
            IPage<ManageUploadBaseQueryResultVO> page=this.baseMapper.selectByAlbumId(new Query<ManageUploadBaseQueryResultVO>().getPage(data), param);
            return ResponseResult.success(PageUtils.getPage(page));
        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }

    }

    @Override
    public ResponseResult deleteImage(ManageBatchDeleteImageDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_ALBUM,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            if(null!=param.getUploadId()){
                this.baseMapper.deleteById(param.getUploadId());
            }
            if(null!=param.getUploadIds()){
                this.baseMapper.deleteImageBatch(param.getUploadIds());
            }


        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success(BizCodeEnume.DELETE_SUCCESS);
    }

    @Override
    @Transactional
    public ResponseResult updateImageName(ManageImageNameUpdateParamDTO param) {

        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_ALBUM,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            userUploadBaseMapper.updateImageName(param);

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }

    @Override
    @Transactional
    public ResponseResult uploadImage(ManageImageUploadParamDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_ALBUM,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            param.setUserId(userPermissionsResult.getUserId());
            param.setShopId(userPermissionsResult.getShopId());
            param.setUploadTime((int) System.currentTimeMillis());

            if (param.getUploadSize() == null) {
                param.setUploadSize(0);
            }
            List<Map<String,String>> path = param.getUploadPath();
            if (param.getUploadId() == null) {
                for (Map<String,String> map : path) {
                    // 设置图片名
                    if (param.getUploadPath() != null) {
                        param.setPath(map.get("url"));
                        param.setUploadName(map.get("name"));
                    }
                    userUploadBaseMapper.insertImage(param);
                }
            } else {
                // 设置图片名
                if (path != null && !path.isEmpty()) {
                    param.setPath(path.get(0).get("url"));
                    param.setUploadName(path.get(0).get("name"));
                }
                userUploadBaseMapper.uploadImage(param);
            }

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }

}
